/*
 *@description: 已弃用,请改用mixins_echarts,柱状图公共组件
 *@author: liuchenguang
 *@date: 2021-12-18
 *@version: V1.0.0
 选填数据：
 data:
  barData: 柱状图数据
 methods:
  addChartEvent: 添加初始化操作
*/
import echarts from 'echarts'
import { debounce } from 'tap-ui/src/utils/index'

export const mixin_echarts_bar = {
  data() {
    return {
      chartInstance: {},
      barData: {
        title: {}, // 标题
        legend: {}, // 图例
        series: [], // 数据
        xAxis: {}, // 横坐标数据
        yAxis: {}, // Y轴
        grid: {}, // 间隔
        dataZoom: {} // 缩放组件
      },
      // 窗口大小变化时，刷新的间隔频率，单位毫秒
      resizeWaitTime: 200
    }
  },
  methods: {
    drawChart(options, id) {
      if (document.getElementById(id)) {
        this.chartInstance = echarts.init(document.getElementById(id), 'light')
        // 添加自定义事件
        this.addChartEvent(this.chartInstance)
        this.chartInstance.setOption(options)
        this.chartInstance.resize()
      }
    },
    /** 添加自定义事件 */
    addChartEvent(myChart) {},
    /** 清空数据 */
    clearChart() {
      if (this.chartInstance.clear) {
        this.chartInstance.clear()
      }
    },
    /** 添加高度自适应事件 */
    addResizeEvent() {
      const resize = () => {
        if (this.chartInstance && typeof this.chartInstance.resize === 'function') {
          this.chartInstance.resize()
        }
      }
      // 防抖处理
      const debounceResize = debounce(resize, this.resizeWaitTime)
      window.addEventListener('resize', debounceResize)
      this.$once('hook:beforeDestroy', () => {
        window.removeEventListener('resize', debounceResize)
      })
    }
  },
  mounted() {
    this.addResizeEvent()
  }
}
